//
// Created by dgy on 2022/12/9.
//
#include <queue>
#include "vector"
#include "iostream"
using namespace std;
struct TreeNode{
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode():val(0),left(nullptr),right(nullptr){}
    TreeNode(int x):val(x),left(nullptr),right(nullptr){}
    TreeNode(int x,TreeNode *left,TreeNode *right):val(x),left(left),right(right){}
};
class Solution{
public:
    /*
     * 深度遍历每一条路径，当最终符合给定的targetsum值的时候表示找到，加入到二维数组中
     * */
    vector<vector<int>> ret;
    vector<int> tar;
    void dfs(TreeNode* root,int target){
        if (root== nullptr||(root->right== nullptr&&root->left== nullptr))
            return;
        target-=root->val;
        tar.emplace_back(root->val);
        if (root->left== nullptr&&root->right== nullptr&&target==0){
            ret.emplace_back(tar);
        }

        dfs(root->left,target);
        dfs(root->right,target);
        tar.pop_back();
    }
    vector<vector<int>> pathSum(TreeNode* root,int target){
        dfs(root,target);
        return ret;
    }
};
